/*
 *  mm_def.cpp
 *  
 *
 *  Created by Bongju Kim on 10. 6. 13..
 *  Copyright 2010 thinkware. All rights reserved.
 *
 */



#include <mm_def.h>


///////////////////// Class CAutoCriticalSection /////////////////////////////
#ifdef POSIX_MUTEX
CAutoCriticalSection::CAutoCriticalSection()
{
	m_pCriticalSection = NULL;
	m_bLocked = false;

	__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "CAutoCriticalSection()##1");
}

CAutoCriticalSection::CAutoCriticalSection(pthread_mutex_t* pCriticalSection, bool enterCriticalSection )
{
	m_pCriticalSection = pCriticalSection;

	if( enterCriticalSection )
	{
		int nRet = EnterCriticalSection(m_pCriticalSection);
		//int nRet = pthread_mutex_lock(m_pCriticalSection);
		m_bLocked = true;

		__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "CAutoCriticalSection(*, Z) : pCriticalSection(%x) nRet(%d)##1", pCriticalSection, nRet);
	}
	else {
		m_bLocked = false;
		__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "CAutoCriticalSection(*, Z) : pCriticalSection(%x) ##2", pCriticalSection);
	}
}

BOOL CAutoCriticalSection::Try(pthread_mutex_t* pCriticalSection)
{
	/*
	BOOL ret = ::TryEnterCriticalSection(pCriticalSection);

	if( ret)
		m_pCriticalSection = pCriticalSection;
	*/
	return TRUE;
}

void CAutoCriticalSection::Lock()
{
	if( !m_bLocked )
	{
		int nRet = EnterCriticalSection(m_pCriticalSection);
		//int nRet = pthread_mutex_lock(m_pCriticalSection);

		m_bLocked = true;
		__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "Lock() : m_pCriticalSection(%x) nRet(%d)##1", m_pCriticalSection, nRet);
	}
	
	__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "Lock() : m_pCriticalSection(%x) ##2", m_pCriticalSection);	
}

void CAutoCriticalSection::Unlock()
{
	if( m_bLocked )
	{
		int nRet = LeaveCriticalSection(m_pCriticalSection);
		//int nRet = pthread_mutex_unlock(m_pCriticalSection);

		m_bLocked = false;
		__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "Unlock() : m_pCriticalSection(%x) nRet(%d)##1", m_pCriticalSection, nRet);
	}
	
	__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "Unlock() : m_pCriticalSection(%x) ##2", m_pCriticalSection);	
}

CAutoCriticalSection::~CAutoCriticalSection()
{
	if( m_pCriticalSection && m_bLocked )
	{
		int nRet = 0;
		__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "~CAutoCriticalSection() : m_pCriticalSection(%x) ##1", m_pCriticalSection);
		nRet = LeaveCriticalSection(m_pCriticalSection);
		//nRet = pthread_mutex_unlock(m_pCriticalSection);
		m_pCriticalSection = NULL;
		m_bLocked = false;

		__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "~CAutoCriticalSection() : m_pCriticalSection(%x) nRet(%d)##2", m_pCriticalSection, nRet);
	}
	
	__android_log_print(ANDROID_LOG_DEBUG, "jni-mapmatch-pthread", "~CAutoCriticalSection() : m_pCriticalSection(%x) ##3", m_pCriticalSection);	
}
#endif


//extern void _my_assert(const char* str_exp, const char* file, int line) {
//	__android_log_print(ANDROID_LOG_ERROR, "ASSERT FAILED!", "(%s:%d) : '%s'", file, line, str_exp);
//}
